<#assign filePath>${project.projectName}/${project.projectName}-biz/src/main/java/${project.basePackage?replace(".","/")}/${model.moduleName?replace(".","/")}/service/impl/</#assign>
<#assign fileName>${model.name?cap_first}ServiceImpl.java</#assign>
package ${project.basePackage}.${module.moduleName}.service.impl;

<#list model.fieldList as field>
    <#if model.primaryFieldId?? && model.primaryFieldId == field.id >
        <#assign primaryField= field>
    </#if>
</#list>
<#assign importPkgs=['lombok.RequiredArgsConstructor','lombok.extern.slf4j.Slf4j','org.springframework.validation.annotation.Validated','org.springframework.stereotype.Service']>
<#assign importPkgs=importPkgs+[project.basePackage+'.'+module.moduleName+'.model.'+model.name?cap_first]>
<#assign importPkgs=importPkgs+[project.basePackage+'.'+module.moduleName+'.service.'+model.name?cap_first+"Service"]>
<#assign importPkgs=importPkgs+[project.basePackage+'.'+module.moduleName+'.repository.'+model.name?cap_first+"Repository"]>
<#assign importPkgs=importPkgs+[project.basePackage+'.'+module.moduleName+'.convert.'+model.name?cap_first+"Converter"]>
<#list model.methodList as method>
    <#if method.type == "CREATE_CMD">
        <#assign hasCmd= true>
        <#assign importPkgs=importPkgs+[project.basePackage+'.'+module.moduleName+'.model.'+model.name?cap_first+method.name?cap_first+'Cmd']>
        <#if !importPkgs?seq_contains('jakarta.validation.Valid')>
            <#assign importPkgs=importPkgs+['jakarta.validation.Valid']>
        </#if>
        <#if !importPkgs?seq_contains('org.springframework.transaction.annotation.Transactional')>
            <#assign importPkgs=importPkgs+['org.springframework.transaction.annotation.Transactional']>
        </#if>
    </#if>
    <#if method.type == "MODIFY_CMD" && primaryField??>
        <#assign hasCmd= true>
        <#assign importPkgs=importPkgs+[project.basePackage+'.'+module.moduleName+'.model.'+model.name?cap_first+method.name?cap_first+'Cmd']>
        <#if !importPkgs?seq_contains('jakarta.validation.Valid')>
            <#assign importPkgs=importPkgs+['jakarta.validation.Valid']>
        </#if>
        <#if !importPkgs?seq_contains('com.fa4j.common.base.exception.CommonRequestError')>
            <#assign importPkgs=importPkgs+['com.fa4j.common.base.exception.CommonRequestError']>
        </#if>
        <#if !importPkgs?seq_contains('org.springframework.transaction.annotation.Transactional')>
            <#assign importPkgs=importPkgs+['org.springframework.transaction.annotation.Transactional']>
        </#if>
    </#if>
    <#if method.type == "REMOVE_CMD" && primaryField??>
        <#assign hasCmd= true>
        <#assign importPkgs=importPkgs+['com.fa4j.common.base.model.RemoveCmd']>
        <#if !importPkgs?seq_contains('jakarta.validation.Valid')>
            <#assign importPkgs=importPkgs+['jakarta.validation.Valid']>
        </#if>
        <#if !importPkgs?seq_contains('com.fa4j.common.base.exception.CommonRequestError')>
            <#assign importPkgs=importPkgs+['com.fa4j.common.base.exception.CommonRequestError']>
        </#if>
        <#if !importPkgs?seq_contains('org.springframework.transaction.annotation.Transactional')>
            <#assign importPkgs=importPkgs+['org.springframework.transaction.annotation.Transactional']>
        </#if>
    </#if>
</#list>
<#if !(hasCmd??) ><#stop></#if>
<#list importPkgs?filter(p->!p?starts_with("java.lang"))?sort as pkg>
import ${pkg};
</#list>

/**
 * 服务实现-${model.remark}
 *
 * @since ${.now?string("yyyy-MM-dd HH:mm:ss")}
 */
@Slf4j
@Service
@Validated
@RequiredArgsConstructor
public class ${model.name?cap_first}ServiceImpl implements ${model.name?cap_first}Service {

    private final ${model.name?cap_first}Repository ${model.name}Repository;
    private final ${model.name?cap_first}Converter ${model.name}Converter;

    <#list model.methodList as method>
        <#if method.type == "CREATE_CMD">
    /**
     * ${model.remark}-${method.remark}
     *
     * @param cmd ${method.remark}参数
     * @return ${model.remark}
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ${model.name?cap_first} ${method.name}(@Valid ${model.name?cap_first}${method.name?cap_first}Cmd cmd) {
        ${model.name?cap_first} entity = ${model.name}Converter.toEntity(cmd);
        ${model.name}Repository.create(entity);
        return entity;
    }
        </#if>
        <#if method.type == "MODIFY_CMD" && primaryField??>
    /**
     * ${model.remark}-${method.remark}
     *
     * @param cmd ${method.remark}参数
     * @return ${model.remark}
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ${model.name?cap_first} ${method.name}(@Valid ${model.name?cap_first}${method.name?cap_first}Cmd cmd) {
        ${model.name?cap_first} entity = ${model.name}Repository.findById(cmd.getId())
                .orElseThrow(() -> CommonRequestError.NOT_FOUND.build("${model.remark}"));
        ${model.name}Converter.updateEntity(entity, cmd);
        ${model.name}Repository.modify(entity);
        return entity;
    }
        </#if>
        <#if method.type == "REMOVE_CMD" && primaryField??>
    /**
     * ${model.remark}-${method.remark}
     *
     * @param cmd ${method.remark}参数
     * @return ${model.remark}
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ${model.name?cap_first} remove(@Valid RemoveCmd<${primaryField.type.type.simpleName}> cmd) {
        ${model.name?cap_first} entity = ${model.name}Repository.findById(cmd.getId())
                .orElseThrow(() -> CommonRequestError.NOT_FOUND.build("${model.remark}"));
        <#list model.fieldList as field>
            <#if field.name=='operatorId'>
        entity.setOperatorId(cmd.getOperatorId());
            </#if>
        </#list>
        ${model.name}Repository.remove(entity);
        return entity;
    }
        </#if>

    </#list>

}






